package com.hjm.project.toolsReward.toolsReward.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.project.system.dict.domain.DictData;
import com.hjm.project.system.dict.mapper.DictDataMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.project.toolsReward.toolsReward.mapper.ToolsRewardMapper;
import com.hjm.project.toolsReward.toolsReward.domain.ToolsReward;
import com.hjm.project.toolsReward.toolsReward.service.IToolsRewardService;
import com.hjm.common.utils.text.Convert;

/**
 * 机具采购奖励标准Service业务层处理
 * 
 * @author LOVE
 * @date 2020-11-23
 */
@Service
public class ToolsRewardServiceImpl implements IToolsRewardService 
{
    @Autowired
    private ToolsRewardMapper toolsRewardMapper;
    @Autowired
    private DictDataMapper dictDataMapper;

    /**
     * 查询机具采购奖励标准
     * 
     * @param id 机具采购奖励标准ID
     * @return 机具采购奖励标准
     */
    @Override
    public ToolsReward selectToolsRewardById(Long id)
    {
        return toolsRewardMapper.selectToolsRewardById(id);
    }

    /**
     * 查询机具采购奖励标准列表
     * 
     * @param toolsReward 机具采购奖励标准
     * @return 机具采购奖励标准
     */
    @Override
    public List<ToolsReward> selectToolsRewardList(ToolsReward toolsReward)
    {
        return toolsRewardMapper.selectToolsRewardList(toolsReward);
    }

    /**
     * 新增机具采购奖励标准
     * 
     * @param params 机具采购奖励标准
     * @return 结果
     */
    @Override
    public AjaxResult insertToolsReward(Map<String,String> params)
    {
        List<DictData> dictDatas = (List<DictData>)dictDataMapper.selectDictDataByType("tools_reward_gears");
        List<ToolsReward> list=new ArrayList<>();
        for (DictData dictData : dictDatas) {
            ToolsReward toolsReward=new ToolsReward();
            if (StringUtils.isNotBlank(params.get("id"+dictData.getDictValue()))){
                toolsReward.setId(Long.parseLong(params.get("id"+dictData.getDictValue())));
            }
            toolsReward.setToolsNumber(Long.parseLong(params.get("toolsNumber"+dictData.getDictValue())));
            toolsReward.setRewardMoney(params.get("rewardMoney"+dictData.getDictValue()));
            if (StringUtils.isNotBlank(params.get("gears"+dictData.getDictValue()))){
                toolsReward.setGears(params.get("gears"+dictData.getDictValue()));
            }else{
                toolsReward.setGears(dictData.getDictValue());
            }
            toolsReward.setAddMan(params.get("addMan"));
            list.add(toolsReward);
        }
        if (list.size()>0){
            for (ToolsReward reward : list) {
                if (null!=reward.getId()){
                    toolsRewardMapper.updateToolsReward(reward);
                }else{
                    toolsRewardMapper.insertToolsReward(reward);
                }
            }
            return AjaxResult.success();
        }else{
            return AjaxResult.error("请填写数据");
        }
    }

    /**
     * 修改机具采购奖励标准
     * 
     * @param toolsReward 机具采购奖励标准
     * @return 结果
     */
    @Override
    public int updateToolsReward(ToolsReward toolsReward)
    {
        return toolsRewardMapper.updateToolsReward(toolsReward);
    }

    /**
     * 删除机具采购奖励标准对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteToolsRewardByIds(String ids)
    {
        return toolsRewardMapper.deleteToolsRewardByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除机具采购奖励标准信息
     * 
     * @param id 机具采购奖励标准ID
     * @return 结果
     */
    @Override
    public int deleteToolsRewardById(Long id)
    {
        return toolsRewardMapper.deleteToolsRewardById(id);
    }
}
